package com.fuda.trade.party3.guosen

import android.util.Log
import com.fuda.trade.model.TradeOrderData
import com.fuda.trade.Party3AppManager
import com.fuda.trade.TradeAccessibilityService
import com.fuda.trade.logElapsedTime
import com.fuda.trade.model.TradeOrder
import com.fuda.trade.startParty3App

class GuosenAppHandler : Party3AppManager.Party3AppHandler {

    companion object{
        val TAG: String = GuosenAppHandler::class.java.simpleName

    }

    override fun handleOrder(service: TradeAccessibilityService, tradeOrder: TradeOrder):Boolean {
        val startTime = System.currentTimeMillis()
        var result = false
        tradeOrder.data?.let {
            when (it){
                is TradeOrderData.Buy -> {
                    Log.i(TAG, "handleCommand tradeOrderData= $tradeOrder")

                    result = startApp(service)

                    if (result) {
                        result = service.waitForViewAndClick("com.guosen.android:id/cb_main_tab_trade", 10)
                    }
                    if (result) {
                        result = service.waitForViewAndClick("com.guosen.android:id/tvLogin", 10)
                    }else{

                    }

                }
                is TradeOrderData.Sell ->  {
                    Log.i(TAG, "handleCommand tradeOrderData= $tradeOrder")
                }

            }

        }

        logElapsedTime(TAG, startTime)
        return result
    }

    override fun refreshActiveForeground(service: TradeAccessibilityService):Boolean {
        TODO("Not yet implemented")
    }

    override fun isAppActive(service: TradeAccessibilityService): Boolean {
        TODO("Not yet implemented")
    }


    fun startApp(service: TradeAccessibilityService): Boolean {
        var result = false
        result = service.globalGoBack()
        if (result) {
            result = service.globalGoHome()
        }
        if (result) {
            // 1. 初始化启动应用
            result = startParty3App(service.applicationContext,"com.guosen.android", "com.guosen.app.goldsun.ui.UserLoginFlashAct")
        }
        return result
    }

}